package com.travel.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.travel.core.AbstractDao.Direction;
import com.travel.core.DaoFactory;
import com.travel.dao.IHotelDao;
import com.travel.dao.ILocalSpecialGoodsDao;
import com.travel.dao.IRestaurantDao;
import com.travel.dao.ISpotDao;
import com.travel.entities.GoodsEntity;
import com.travel.entities.Hotel;
import com.travel.entities.Restaurant;
import com.travel.entities.Spot;

public class TopHotServlet extends HttpServlet {

    private static final long serialVersionUID = 7736449402556864109L;

    private static final Logger LOGGER = LogManager.getLogger(TopHotServlet.class);

    private static final IHotelDao HotelDao = DaoFactory.getDao(IHotelDao.class);

    private static final ISpotDao SpotDao = DaoFactory.getDao(ISpotDao.class);

    private static final IRestaurantDao RestaurantDao = DaoFactory.getDao(IRestaurantDao.class);

    private static final ILocalSpecialGoodsDao LocalSpecialGoodsDao = DaoFactory.getDao(ILocalSpecialGoodsDao.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String currLoc = request.getParameter("loc");
        String infoType = request.getParameter("infoType");
        if (StringUtils.isBlank(currLoc)) {
            currLoc = "0773";// 默认显示桂林
        }
        LOGGER.info("是否有参数传递：" + currLoc + "||" + infoType);

        JSONObject queryResultJSON = new JSONObject();
        if ("01".equals(infoType)) {// 旅游景点游数据
            List<Spot> spots1 = SpotDao.getTopSpots(currLoc, "spotAvgRating", Direction.DESC);
            List<Spot> spots2 = SpotDao.getTopSpots(currLoc, "spotRatingCount", Direction.DESC);

            JSONArray jsonArray11 = new JSONArray();
            JSONArray jsonArray12 = new JSONArray();
            for (Spot u : spots1) {
                JSONObject json = new JSONObject();
                try {
                    json.put("spotName", u.getSpotName());
                    json.put("spotId", u.getSpotId());
                    json.put("spotUrl", u.getSpotUrl());
                    json.put("spotAvgRating", u.getSpotAvgRating());
                    json.put("spotRatingCount", u.getSpotRatingCount());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }
                jsonArray11.put(json);
            }
            for (Spot u : spots2) {
                JSONObject json = new JSONObject();
                try {
                    json.put("spotName", u.getSpotName());
                    json.put("spotId", u.getSpotId());
                    json.put("spotUrl", u.getSpotUrl());
                    json.put("spotAvgRating", u.getSpotAvgRating());
                    json.put("spotRatingCount", u.getSpotRatingCount());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }
                jsonArray12.put(json);
            }

            queryResultJSON.put("spotAvgRatingQuery", jsonArray11);
            queryResultJSON.put("spotRatingCountQuery", jsonArray12);
        } else if ("02".equals(infoType)) {// 餐厅数据
            List<Restaurant> restaurants1 = RestaurantDao.getTopResturant(currLoc, "taste", Direction.DESC, 10);
            List<Restaurant> restaurants2 = RestaurantDao.getTopResturant(currLoc, "environment", Direction.DESC, 10);
            List<Restaurant> restaurants3 = RestaurantDao.getTopResturant(currLoc, "service", Direction.DESC, 10);

            JSONArray jsonArray21 = new JSONArray();
            for (Restaurant u : restaurants1) {
                JSONObject json = new JSONObject();
                try {
                    json.put("shopName", u.getShopName());
                    json.put("shopUrl", u.getShopUrl());
                    json.put("shopAddress", u.getShopAddress());
                    json.put("shopTell", u.getShopTell());
                    json.put("shopOpenTime", u.getShopOpenTime());
                    json.put("shopPersonAve", u.getShopPersonAve());
                    json.put("shopTaste", u.getShopTaste());
                    json.put("shopEnvironment", u.getShopEnvironment());
                    json.put("shopService", u.getShopService());
                    json.put("shopStar", u.getShopStar());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }
                jsonArray21.put(json);
            }

            JSONArray jsonArray22 = new JSONArray();
            for (Restaurant u : restaurants2) {
                JSONObject json = new JSONObject();
                try {
                    json.put("shopName", u.getShopName());
                    json.put("shopUrl", u.getShopUrl());
                    json.put("shopAddress", u.getShopAddress());
                    json.put("shopTell", u.getShopTell());
                    json.put("shopOpenTime", u.getShopOpenTime());
                    json.put("shopPersonAve", u.getShopPersonAve());
                    json.put("shopTaste", u.getShopTaste());
                    json.put("shopEnvironment", u.getShopEnvironment());
                    json.put("shopService", u.getShopService());
                    json.put("shopStar", u.getShopStar());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }
                jsonArray22.put(json);
            }

            JSONArray jsonArray23 = new JSONArray();
            for (Restaurant u : restaurants3) {
                JSONObject json = new JSONObject();
                try {
                    json.put("shopName", u.getShopName());
                    json.put("shopUrl", u.getShopUrl());
                    json.put("shopAddress", u.getShopAddress());
                    json.put("shopTell", u.getShopTell());
                    json.put("shopOpenTime", u.getShopOpenTime());
                    json.put("shopPersonAve", u.getShopPersonAve());
                    json.put("shopTaste", u.getShopTaste());
                    json.put("shopEnvironment", u.getShopEnvironment());
                    json.put("shopService", u.getShopService());
                    json.put("shopStar", u.getShopStar());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }
                jsonArray23.put(json);
            }
            queryResultJSON.put("shopTasteQuery", jsonArray21);
            queryResultJSON.put("shopEnvironmentQuery", jsonArray22);
            queryResultJSON.put("shopServiceQuery", jsonArray23);
        } else if ("03".equals(infoType)) {// 住宿数据
            List<Hotel> hotels1 = HotelDao.selectListOrderBy(currLoc, "comment_point", Direction.DESC);
            List<Hotel> hotels2 = HotelDao.selectListOrderBy(currLoc, "comment_score", Direction.DESC);
            List<Hotel> hotels3 = HotelDao.selectListOrderBy(currLoc, "comment_count", Direction.DESC);

            JSONArray jsonArray31 = new JSONArray();
            for (Hotel u : hotels1) {
                JSONObject json = new JSONObject();
                try {
                    json.put("hotelName", u.getHotelName());
                    json.put("hotelUrl", u.getHotelUrl());
                    json.put("hotelCommentPoint", u.getHotelCommentPoint());
                    json.put("hotelCommentScore", u.getHotelCommentScore());
                    json.put("hotelCommentCount", u.getHotelCommentCount());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }

                jsonArray31.put(json);
            }

            JSONArray jsonArray32 = new JSONArray();
            for (Hotel u : hotels2) {
                JSONObject json = new JSONObject();
                try {
                    json.put("hotelName", u.getHotelName());
                    json.put("hotelUrl", u.getHotelUrl());
                    json.put("hotelCommentPoint", u.getHotelCommentPoint());
                    json.put("hotelCommentScore", u.getHotelCommentScore());
                    json.put("hotelCommentCount", u.getHotelCommentCount());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }

                jsonArray32.put(json);
            }

            JSONArray jsonArray33 = new JSONArray();
            for (Hotel u : hotels3) {
                JSONObject json = new JSONObject();
                try {
                    json.put("hotelName", u.getHotelName());
                    json.put("hotelUrl", u.getHotelUrl());
                    json.put("hotelCommentPoint", u.getHotelCommentPoint());
                    json.put("hotelCommentScore", u.getHotelCommentScore());
                    json.put("hotelCommentCount", u.getHotelCommentCount());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }

                jsonArray33.put(json);
            }

            queryResultJSON.put("commentPointQuery", jsonArray31);
            queryResultJSON.put("commentScoreQuery", jsonArray32);
            queryResultJSON.put("commentCountQuery", jsonArray33);
        } else {// 购数据
            List<GoodsEntity> goodsEntities1 = LocalSpecialGoodsDao.getTopGoods(currLoc, "item_sale", Direction.DESC);
            List<GoodsEntity> goodsEntities2 = LocalSpecialGoodsDao.getTopGoods(currLoc, "item_rate_num", null);
            JSONArray jsonArray41 = new JSONArray();
            for (GoodsEntity u : goodsEntities1) {
                JSONObject json = new JSONObject();
                try {
                    json.put("shopName", u.getShopName());
                    json.put("shopUrl", u.getShopUrl());
                    json.put("itemName", u.getItemName());
                    json.put("itemUrl", u.getItemUrl());
                    json.put("itemSale", u.getItemSale());
                    json.put("itemRateNum", u.getItemRateNum());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }

                jsonArray41.put(json);
            }
            JSONArray jsonArray42 = new JSONArray();
            for (GoodsEntity u : goodsEntities2) {
                JSONObject json = new JSONObject();
                try {
                    json.put("shopName", u.getShopName());
                    json.put("shopUrl", u.getShopUrl());
                    json.put("itemName", u.getItemName());
                    json.put("itemUrl", u.getItemUrl());
                    json.put("itemSale", u.getItemSale());
                    json.put("itemRateNum", u.getItemRateNum());
                } catch (Exception e) {
                    LOGGER.error(e.getLocalizedMessage(), e);
                }

                jsonArray42.put(json);
            }

            queryResultJSON.put("itemSaleQuery", jsonArray41);
            queryResultJSON.put("itemRateNumQuery", jsonArray42);
        }

        response.setCharacterEncoding("utf-8");
        System.out.println(queryResultJSON.toString());
        response.getWriter().append(queryResultJSON.toString());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
